#coding:utf-8


# import csv
# import os
# import numpy as np
# import random
# import requests
# # name of data file
# # 数据集名称
# birth_weight_file = 'birth_weight.csv'
#
# # download data and create data file if file does not exist in current directory
# # 如果当前文件夹下没有birth_weight.csv数据集则下载dat文件并生成csv文件
# if not os.path.exists(birth_weight_file):
#     birthdata_url = 'https://github.com/nfmcclure/tensorflow_cookbook/raw/master/01_Introduction/07_Working_with_Data_Sources/birthweight_data/birthweight.dat'
#     birth_file = requests.get(birthdata_url)
#     birth_data = birth_file.text.split('\r\n')
#     # split分割函数,以一行作为分割函数，windows中换行符号为'\r\n',每一行后面都有一个'\r\n'符号。
#     birth_header = birth_data[0].split('\t')
#     # 每一列的标题，标在第一行，即是birth_data的第一个数据。并使用制表符作为划分。
#     birth_data = [[float(x) for x in y.split('\t') if len(x) >= 1] for y in birth_data[1:] if len(y) >= 1]
#     print(np.array(birth_data).shape)
#     # (189, 9)
#     # 此为list数据形式不是numpy数组不能使用np,shape函数,但是我们可以使用np.array函数将list对象转化为numpy数组后使用shape属性进行查看。
#     with open(birth_weight_file, "w", newline='') as f:
#     # with open(birth_weight_file, "w") as f:
#         writer = csv.writer(f)
#         writer.writerows([birth_header])
#         writer.writerows(birth_data)
#         f.close()

import xlrd

def excel_read(filepath,by_name=u'Sheet1'):
	readbook = xlrd.open_workbook(filepath)
	table = readbook.sheet_by_name(by_name)
	nrows = table.nrows #行数 
	colnames =  table.row_values(1) #字典key所在行，即参数的key 
	list =[]
	for rownum in range(3,nrows):		#数字3 即有效数据开始的行数为第三行（文档的第四行，从0开始算）
		row = table.row_values(rownum)
		if row:
			app = {}
			for i in range(len(colnames)):
				app[colnames[i]] = row[i][1:]
			list.append(app)
	return list


